package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.customer.enums.BankAccontTypeEnum;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 银行账户 服务实现类
 * </p>
 *
 * @author xuchun
 * @since 2024-08-11
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {

    /**
     * 新增或更新银行账号信息
     * @param dto
     */
    @Override
    public void saveBankAccount(BankAccountResDTO dto) {
        BankAccount bankAccount = getById(dto.getId());
        boolean result;
        if(bankAccount == null){
            //新增
            bankAccount = BeanUtil.copyProperties(dto, BankAccount.class);
            bankAccount.setId(UserContext.currentUserId());
            result = save(bankAccount);
        }else {
            //更新
            BeanUtil.copyProperties(dto, bankAccount);
            result = updateById(bankAccount);
        }
        if(!result){
            throw new ForbiddenOperationException("新增或更新银行账号信息失败");
        }
    }

    @Override
    public BankAccountResDTO currentUserBankAccount(Integer type) {
        BankAccount bankAccount = lambdaQuery()
                .eq(BankAccount::getId, UserContext.currentUserId())
                .eq(BankAccount::getType, type)
                .one();
        return BeanUtil.copyProperties(bankAccount, BankAccountResDTO.class);
    }
}
